package com.hr.mes.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hr.mes.dto.Page;
import com.hr.mes.entity.Form;
import com.hr.mes.entity.FormDetail;
import com.hr.mes.entity.FormFields;
import com.hr.mes.entity.FormTemplate;
import com.hr.mes.mapper.FormDetailMapper;
import com.hr.mes.mapper.FormFieldsMapper;
import com.hr.mes.mapper.FormMapper;
import com.hr.mes.mapper.FormTemplateMapper;
import com.hr.mes.service.FirstItemService;
import com.hr.mes.utils.PageUtil;
import com.hr.mes.vo.FormDetailVO;
import com.hr.mes.vo.FormSubmitVO;
import com.hr.mes.vo.FormViewVO;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Transactional
@Service
public class FirstItemServiceImpl implements FirstItemService {

    @Autowired
    private FormFieldsMapper formFieldsMapper;

    @Autowired
    private FormMapper formMapper;

    @Autowired
    private FormDetailMapper formDetailMapper;

    @Autowired
    private FormTemplateMapper formTemplateMapper;

    @Override
    public Page<FormViewVO> getFormSubmitPage(Integer pageNo, Integer pageSize,
                                              String employeeNumber, Integer status, Date startTime, Date endTime) {
        ArrayList<FormViewVO> formViewVOS = new ArrayList<>();
        List<Form> submitForms = formMapper.getFormSubmitList(employeeNumber, status, startTime, endTime);
        for (Form form : submitForms){
            FormViewVO formViewVO = new FormViewVO();
            formViewVO.setFormId(form.getId());
            formViewVO.setFormTemplateId(form.getFormTemplateId());
            formViewVO.setRemark(form.getRemark());
            formViewVO.setSubmitTime(form.getSubmitTime());
            formViewVO.setSubmitBy(form.getSubmitBy());
            formViewVO.setStatus(form.getStatus());
            FormTemplate formTemplate = formTemplateMapper.getFormTemplate(form.getFormTemplateId());
            formViewVO.setTemplateTitle(formTemplate.getTitle());
            formViewVOS.add(formViewVO);
        }
        PageHelper.startPage(pageNo, pageSize);
        PageInfo<FormViewVO> pageInfo = new PageInfo<>(formViewVOS);
        Page<FormViewVO> page = PageUtil.ConvertPageInfoToPage(pageInfo);
        PageHelper.clearPage();
        return page;
    }

    @Override
    public List<FormFields> getFormFields(Integer formTemplateId) {
        List<FormFields> formFieldsList = formFieldsMapper.getFormFields(formTemplateId);
        List<FormFields> collect = formFieldsList.stream().filter(formFields -> formFields.getRepeat() == 0).collect(Collectors.toList());
        return collect;
    }

    @Override
    public List<FormFields> getFormFieldsRepeat(Integer formTemplateId) {
        List<FormFields> formFieldsList = formFieldsMapper.getFormFields(formTemplateId);
        List<FormFields> collect = formFieldsList.stream().filter(formFields -> formFields.getRepeat() == 1).collect(Collectors.toList());
        return collect;
    }

    @Override
    public Integer addFirstItem(FormSubmitVO formSubmitVO) {
        //主表
        Form form = new Form();
        form.setFormTemplateId(formSubmitVO.getFormTemplateId());
        String employeeNumber = (String) SecurityUtils.getSubject().getPrincipal();
        form.setSubmitBy(employeeNumber);
        form.setStatus(1);
        formMapper.addForm(form);

        //具体内容表
        List<FormDetail> formDetails = formSubmitVO.getFormDetails();
        for(FormDetail formDetail : formDetails){
            formDetail.setFormId(form.getId());
        }
        formDetailMapper.addFormDetails(formDetails);

        return form.getId();
    }

    @Override
    public List<FormDetailVO> getFormDetails(Integer formId) {
        List<FormDetailVO> formDetailVOs = formDetailMapper.getFormDetails(formId);
        return formDetailVOs;
    }
}
